package com.shanghai.shanghaielectric.UI.setting;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.Model.db.ErrorInfoModel;
import com.shanghai.shanghaielectric.Model.db.OperateInfoModel;
import com.shanghai.shanghaielectric.MyApplication;
import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.UI.DilaogView.Dialog_Alert;
import com.shanghai.shanghaielectric.UI.FragmentTabAdapter_Main;
import com.shanghai.shanghaielectric.UI.UpdateApkFragment;

import org.litepal.crud.DataSupport;
import org.litepal.crud.callback.UpdateOrDeleteCallback;

import java.util.ArrayList;
import java.util.List;

import shanghaielectric_lib.Utils.dwin.Dwin;


/**
 * 设置界面 Author: liudanhua Date: 15-11-23 Time: 下午2:39
 */
@SuppressLint("NewApi")
public class SettingFragment extends Fragment {
    private Button bt_setting_factory;
    private RadioGroup settingrgs;
    public List<Fragment> settingfragments = new ArrayList<Fragment>();
    public View settingView;
    private SettingParamFragment settingParam;
    private int pause = 0;
    private RadioButton radio_setting_param;
    private Context mContext;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("Setting____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏软键盘
        InputMethodManager imm = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()){
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
        System.out.println("Setting____onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("Setting____onCreateView");
        Constants.isScanState=false;

        settingView = inflater.inflate(R.layout.setting_fragment, container, false);
        pause = 0;
        mContext= MyApplication.getContext().getApplicationContext();
        radio_setting_param = (RadioButton) settingView.findViewById(R.id.radio_setting_param);
        settingrgs = (RadioGroup) settingView.findViewById(R.id.tabs_setting_rg);
        bt_setting_factory = (Button) settingView.findViewById(R.id.bt_setting_factory);

        settingParam = new SettingParamFragment();
        settingfragments.add(settingParam);
        settingfragments.add(new SettingSystemFragment());
        settingfragments.add(new SettingCommunicationFragment());
        settingfragments.add(new UpdateApkFragment());


        FragmentTabAdapter_Main tabAdapter = new FragmentTabAdapter_Main(this.getActivity(), settingfragments, R.id.tab_setting_content, settingrgs);
        tabAdapter.setOnRgsExtraCheckedChangedListener(new FragmentTabAdapter_Main.OnRgsExtraCheckedChangedListener() {
            @Override
            public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {
                System.out.println("Extra---- " + index + " checked!!! ");
            }
        });

        bt_setting_factory.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDiolg("是否恢复出厂设置\r\n（系统重启）");

                Dialog_Alert.setDiolgAlertListener(new Dialog_Alert.DialogAlertListener() {
                    @Override
                    public void onClink(boolean flag) {
                        if (flag) {
                            Toast.makeText(mContext, "系统重启中请稍后...", Toast.LENGTH_SHORT).show();
                            DataSupport.deleteAllAsync(OperateInfoModel.class).listen(new UpdateOrDeleteCallback() {
                                @Override
                                public void onFinish(int rowsAffected) {
                                    DataSupport.deleteAllAsync(ErrorInfoModel.class).listen(new UpdateOrDeleteCallback() {
                                        @Override
                                        public void onFinish(int rowsAffected) {
                                            Dwin.reboot();
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            }
        });
        return settingView;
    }
    private void showDiolg(String commandName) {
        Dialog_Alert diolgAlert = new Dialog_Alert();
        diolgAlert.setCommandName(commandName);
        diolgAlert.show(this.getFragmentManager(), "");
    }
//    private void showDiolg(String commandName) {
//        Dialog_Alert_Control_Command diolg_Alert_Control_Command = new Dialog_Alert_Control_Command();
//        diolg_Alert_Control_Command.setCommandName(commandName);
//        diolg_Alert_Control_Command.show(this.getFragmentManager(), "");
//    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("Setting____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("Setting____onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        settingParam.onResume();
//        if (pause == 1) {
//            pause = 0;
//            radio_setting_param.setChecked(true);
//            if (settingParam.isAdded()) {
//                settingParam.onResume(); // 启动目标tab的onResume()
//            }
//        }
        System.out.println("Setting____onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        pause = 1;
        if (settingParam.isAdded()) {
            settingParam.onPause(); // 启动目标tab的onResume()
        }
        System.out.println("Setting____onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("Setting____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("Setting____onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Setting____onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("Setting____onDetach");
    }

}
